<?php

class Url {

  var $config = array();

  function __construct($config=array()) {
    $this->config['rewrite'] = false;
    $this->config['encrypt'] = false;
    $this->setconfig($config);      
  }

  function __destruct() {

  }

  function setconfig($config=array()) {
    if (!empty($config) && is_array($config)){
      foreach($config as $key=>$value) {
        $this->config[$key] = $value;
      }
    }
    if($this->config['encrypt']) $this->config['rewrite'] = true;
    if($this->config['rewrite']) C('Template')->setrepath(true);  
  }

  function get($key) {
    return $this->config[$key];
  }
  
  
  function create($arr) {
    return $this->config['rewrite'] ? $this->encrypt($arr) : $this->normal_create($arr);
  }
  
  function parser($url) {
    return $this->config['rewrite'] ? $this->decrypt($url) : $this->normal_parser($url);
  }
  
  
  function normal_create($arr) {
    if(!is_array($arr)) return;
    $url = 'app='.$arr['app'].'&method='.$arr['method'];
    $par = '';
    foreach($arr['params'] as $key=>$val) {
      $par .= '&'.$key.'='.urlencode($val);
    }
    $url .= $par;
    return WEB_SURL.'?'.$url;
  }
  
  function normal_parser($url='') {
    $return = array(
      'app'    => $_REQUEST['app'],
      'method' => $_REQUEST['method'],
      'params' => array()
    );
    foreach($_REQUEST as $key=>$val) {
      if($key!='app' && $key!='method')
         $return['params'][$key] = urldecode($val);
      }
    return $return;
  }

  function encrypt($arr) {
    if(!is_array($arr)) return;
    $url = $arr['app'].'--'.$arr['method'].'--';
    $par = '';
    foreach($arr['params'] as $key=>$val) {
      $par .= $key.'-'.urlencode($val).'-';
    }
    if(!$par) $url = substr($url,0,-2);
    else $url .= substr($par,0,-1);
    return $this->config['encrypt'] ? WEB_SURL.'/'.encrypt($url).'.html' : WEB_SURL.'/'.$url.'.html';
  }
  
  function decrypt($url='') {
    if(!$url) $url = WEB_URI;
    if(strpos($url,'?') or preg_match('/.+\.php$/',$url)) {
      C('Template')->setrepath(false);
      return $this->normal_parser($url);
    }
    $url = preg_replace("/.*\/([^\/]+)$/", "\\1", $url);
    $url = str_replace('.html', '', $url);
    $url = $this->config['encrypt'] ? decrypt($url) : $url;
    $url = $this->trimstr($url);
    list($app,$method,$var) = explode('--',$url);
    $return = array(
      'app'    => $_REQUEST['app'] ? $_REQUEST['app'] : $app,
      'method' => $_REQUEST['method'] ? $_REQUEST['method'] : $method,
      'params' => array()
    );
    $params = explode('-',$var);
    if(is_array($params)) {
      $t = count($params);
      for($i=0;$i<=$t;$i+=2) {
        if($params[$i]) {
          $return['params'][$params[$i]] = $_REQUEST[$params[$i]] ? $_REQUEST[$params[$i]] : urldecode($params[$i+1]);
        }
      }
    }
    return $return;
  }

  
  function torequest($url='') {
    $arr = $this->parser($url);
    $_REQUEST['app']    = $arr['app']; 
    $_REQUEST['method'] = $arr['method'];
    foreach($arr['params'] as $key=>$val) {
      $_REQUEST[$key] = $val; 
    }
  }
  
  
  function trimstr($v) {
    return str_replace(array('"',"'",';'),array('','',''),$v);
  }
  
}

?>